/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.order.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.modules.anny.wxuser.entity.AnnyWxUser;
import com.jeeplus.modules.wxutil.WeiXinUserInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.anny.order.entity.AnnyOrder;
import com.jeeplus.modules.anny.order.service.AnnyOrderService;

/**
 * 订单管理Controller
 * @author clf
 * @version 2018-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/order/annyOrder")
public class AnnyOrderController extends BaseController {

	AjaxJson j = new AjaxJson();

	@Autowired
	private AnnyOrderService annyOrderService;

	@ModelAttribute
	public AnnyOrder get(@RequestParam(required=false) String id) {
		AnnyOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyOrderService.get(id);
		}
		if (entity == null){
			entity = new AnnyOrder();
		}
		return entity;
	}

	/**
	 * 订单信息列表页面
	 */
	@RequiresPermissions("order:annyOrder:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/anny/order/annyOrderList";
	}

	/**
	 * 订单信息列表数据
	 */
	@ResponseBody
	@RequiresPermissions("order:annyOrder:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyOrder annyOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AnnyOrder> page = annyOrderService.findPage(new Page<AnnyOrder>(request, response), annyOrder);
        List<AnnyOrder> list = page.getList();
        for (int i=0;i<list.size();i++){
            System.out.println(list.get(i).getShopId());
        }
        return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑订单信息表单页面
	 */
	@RequiresPermissions(value={"order:annyOrder:view","order:annyOrder:add","order:annyOrder:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyOrder annyOrder, Model model) {
		model.addAttribute("annyOrder", annyOrder);
		if(StringUtils.isBlank(annyOrder.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/order/annyOrderForm";
	}

	/**
	 * 保存订单信息
	 */
	@RequiresPermissions(value={"order:annyOrder:add","order:annyOrder:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyOrder annyOrder, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyOrder)){
			return form(annyOrder, model);
		}
		//新增或编辑表单保存
		annyOrderService.save(annyOrder);//保存
		addMessage(redirectAttributes, "保存订单信息成功");
		return "redirect:"+Global.getAdminPath()+"/order/annyOrder/?repage";
	}

	/**
	 * 删除订单信息
	 */
	@ResponseBody
	@RequiresPermissions("order:annyOrder:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyOrder annyOrder, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyOrderService.delete(annyOrder);
		j.setMsg("删除订单信息成功");
		return j;
	}

	/**
	 * 批量删除订单信息
	 */
	@ResponseBody
	@RequiresPermissions("order:annyOrder:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyOrderService.delete(annyOrderService.get(id));
		}
		j.setMsg("删除订单信息成功");
		return j;
	}

	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("order:annyOrder:export")
	@RequestMapping(value = "export", method=RequestMethod.POST)
	public AjaxJson exportFile(AnnyOrder annyOrder, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			String fileName = "订单信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<AnnyOrder> page = annyOrderService.findPage(new Page<AnnyOrder>(request, response, -1), annyOrder);
			new ExportExcel("订单信息", AnnyOrder.class).setDataList(page.getList()).write(response, fileName).dispose();
			j.setSuccess(true);
			j.setMsg("导出成功！");
			return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出订单信息记录失败！失败信息："+e.getMessage());
		}
		return j;
	}

	@ResponseBody
	@RequestMapping(value = "detail")
	public AnnyOrder detail(String id) {
		return annyOrderService.get(id);
	}


	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("order:annyOrder:import")
	@RequestMapping(value = "import", method=RequestMethod.POST)
	public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<AnnyOrder> list = ei.getDataList(AnnyOrder.class);
			for (AnnyOrder annyOrder : list){
				try{
					annyOrderService.save(annyOrder);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条订单信息记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条订单信息记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入订单信息失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/order/annyOrder/?repage";
	}

	/**
	 * 下载导入订单信息数据模板
	 */
	@RequiresPermissions("order:annyOrder:import")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "订单信息数据导入模板.xlsx";
			List<AnnyOrder> list = Lists.newArrayList();
			new ExportExcel("订单信息数据", AnnyOrder.class, 1).setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/order/annyOrder/?repage";
	}




}